package cn.sjtu.algorithm.array_sort;

import java.util.Arrays;

/**
 * @author syo 2319615164@qq.com
 * @description 快速排序
 * @date 2024/5/16
 * @Copyright syo
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] nums = {9, 0, 1, 8, 2, 7, 3, 6, 4, 5};
        quickSort(nums, 0, nums.length - 1);

        System.out.println(Arrays.toString(nums));
    }

    public static void quickSort(int[] nums, int l, int r) {
        if (l >= r) {
            return;
        }

        int mid = partition(nums, l, r);
        quickSort(nums, l, mid - 1);
        quickSort(nums, mid + 1, r);
    }

    public static int partition(int[] nums, int l, int r) {
        int pov = nums[l];
        int i = l;
        for (int j = l; j < r; j++) {
            if (nums[j] <= pov) {
                swap(nums, i, j);
                i++;
            }
        }
        swap(nums, i, r);
        return i;
    }

    public static void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
